<template>
  <div>
    <!-- 选择部门 -->
    <el-form
      ref="overtimeForm"
      :model="overtimeBase"
      label-position="right"
      label-width="80px"
      class="titmInfo"
    >
      <el-form-item label="部门：" prop="departmentId">
        <el-select
          v-model="value"
          placeholder="请选择"
          @change="overTimeByDepartId"
        >
          <el-option
            v-for="item in departmentData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="加班规则：">
        <div v-for="item in overtimeBase.rules" :key="item.id" class="ruleInfo">
          <el-row>
            <el-col :span="8">
              <div class="grid-content bg-purple">
                <el-switch v-model="item.isEnable" />
                &nbsp;&nbsp;{{ item.rule }}
              </div>
            </el-col>
            <el-col :span="16">
              <div class="grid-content bg-purple-light">
                <span class="pad">
                  <el-checkbox v-model="item.isTimeOff">调休假</el-checkbox>
                </span>
                <template>
                  <el-time-select
                    v-model="item.ruleStartTime"
                    :picker-options="{
                      start: '01:00',
                      step: '00:15',
                      end: '23:59'
                    }"
                    style="width:100px;"
                    :disabled="item.isTimeOff===false"
                  />
                  <el-time-select
                    v-model="item.ruleEndTime"
                    :picker-options="{
                      start: '01:00',
                      step: '00:15',
                      end: '23:59',
                      minTime: item.startTime
                    }"
                    style="width:100px;"
                    :disabled="item.isTimeOff===false"
                  />
                </template>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-form-item>
      <el-form-item label="打卡验证：" prop="isClock">
        <el-switch v-model="overtimeBase.isClock" />&nbsp;&nbsp;加班需要有打卡记录
      </el-form-item>
      <el-form-item label="开启补偿：" prop="isCompensationint">
        <el-switch v-model="overtimeBase.isCompensationint" />
      </el-form-item>
      <el-form-item label="调休假设置：" prop="latestEffectDate">
        <div class="ruleInfo">
          最晚有效期： 次年
          <el-date-picker
            v-model="overtimeBase.latestEffectDate"
            type="date"
            placeholder="选择日期"
            style="width:150px;"
          />
        </div>
      </el-form-item>
      <el-form-item label prop="unit">
        <div class="ruleInfo">
          <p>
            请假最小单位
            <el-input v-model="overtimeBase.unit" style="width:50px" />天
          </p>
        </div>
      </el-form-item>

    </el-form>

    <!-- 按钮 -->
    <div class="el-dialog__footer dialog-footer">
      <el-button type="primary" @click="handleOvertime">保存更新</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { overTimeByDepartId, overtimeSave } from '@/api/attendances'
import attendanceApi from '@/api/constant/attendance'
import * as commonApi from '@/utils'
export default {
  props: {
    showSetting: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      value: '总裁办',
      // 部门列表数据
      departmentData: [],
      // 存储加班列表数据
      durationList: [],

      overtimeBase: {
        departmentId: '',
        isClock: false,
        isCompensationint: false,
        latestEffectDate: '',
        unit: '',
        rules: attendanceApi.overtimeType
      },
      formBase: {
        morningStartTime: '',
        morningEndTime: '',
        afternoonStartTime: '',
        afternoonEndTime: '',
        departmentId: ''
      },
      // 设置完毕后保存的数据
      deductionList: [],
      // 加班校验规则
      rule: {
        overtimeRule: {
        // 表单验证
          unit: [
            { required: true, message: '调休单位不能为空(', trigger: 'blur' }
          ],
          departmentId: [
            { required: true, message: '请选择部门', trigger: 'change' }
          ],
          isClock: [
            { required: true, message: '请选择打卡验证', trigger: 'change' }
          ],
          isCompensationint: [
            { required: true, message: '请选择补偿', trigger: 'change' }
          ],
          latestEffectDate: [
            { required: true, message: '请选择时间', trigger: 'change' }
          ]
        }
      }
    }
  },
  created() {
    this.getDepartments()
    this.stateData = attendanceApi
  },
  methods: {
    //   获取部门列表
    async getDepartments() {
      const { depts } = await getDepartments()
      this.departmentData = depts
    },
    // 根据部门 id 加班选择部门
    async overTimeByDepartId(val) {
      this.overtimeBase.departmentId = val
      this.overtimeBase.rules.forEach(item => {
        item.departmentId = val
        item.isEnable = item.isTimeOff = false
        item.ruleEndTime = item.ruleStartTime = ''
      })
      this.overtimeBase.latestEffectDate = ''
      this.overtimeBase.unit = ''
      this.overtimeBase.isClock = this.overtimeBase.isCompensationint = false
      const data = await overTimeByDepartId({ departmentId: val })
      if (data.dayOffConfigs !== null || data.extraDutyConfig !== null) {
        this.overtimeBase.departmentId = data.dayOffConfigs.departmentId
        this.overtimeBase.latestEffectDate =
              data.dayOffConfigs.latestEffectDate
        this.overtimeBase.unit = data.dayOffConfigs.unit
        this.overtimeBase.isClock = data.extraDutyConfig.isClock
        this.overtimeBase.isCompensationint =
              data.extraDutyConfig.isCompensationint
      }
      if (this.overtimeBase.isClock === 0) {
        this.overtimeBase.isClock = true
      } else {
        this.overtimeBase.isClock = false
      }
      if (this.overtimeBase.isCompensationint === 0) {
        this.overtimeBase.isClock = true
      } else {
        this.overtimeBase.isCompensationint = false
      }
      if (data.extraDutyRuleList.length > 0) {
        data.extraDutyRuleList.forEach(item => {
          if (item.isEnable === 0) {
            item.isEnable = true
          } else {
            item.isEnable = false
          }
          if (item.isTimeOff === 0) {
            item.isTimeOff = true
          } else {
            item.isTimeOff = false
          }
        })
        this.overtimeBase.rules = data.extraDutyRuleList
      }
    },
    // 确认按钮
    // 加班配置保存更新
    handleOvertime() {
      this.$refs.overtimeForm.validate(async res => {
        if (res) {
          var deductionList = this.overtimeBase
          deductionList.latestEffectDate = commonApi.transListToTreeData(
            deductionList.latestEffectDate
          )
          if (deductionList.isClock === true) {
            deductionList.isClock = '0'
          } else {
            deductionList.isClock = '1'
          }
          if (deductionList.isCompensationint === true) {
            deductionList.isCompensationint = '0'
          } else {
            deductionList.isCompensationint = '1'
          }
          deductionList.rules.forEach(item => {
            if (item.isEnable === true) {
              item.isEnable = '0'
            } else {
              item.isEnable = '1'
            }
            if (item.isTimeOff === true) {
              item.isTimeOff = '0'
            } else {
              item.isTimeOff = '1'
            }
          })
          await overtimeSave(deductionList)
          this.$message.success('操作成功')
          this.handleClose()
        }
      })
    },
    // 取消按钮
    handleClose() {
      // 清空表单数据
      this.$refs.overtimeForm.resetFields()
      // 关闭弹窗
      this.$emit('update:showSetting', false)
    }
  }
}
</script>

<style lang="scss" scoped>

.set{
  border: solid 1px #ccc;
      border-radius: 3px;
      padding: 15px 30px 15px 20px;
      position: relative;
      display: inline-block;
      margin-right: 20px;
  span:first-child {
        padding-right: 15px;
        border-right: solid 1px #ccc;
        margin-right: 15px;
      }
      i{
        position: absolute;
        top: 5px;
        right: 5px;
      }
}
</style>
